package c.main.util;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.security.MessageDigest;
 
/**
 * MD5加密算法，支持加盐，可计算指定文件的md5值
 * @author 刘军海
 * @version May 2, 2012 10:35:00 AM
 */
public final class MD5 {
    private final static String[] hexDigits = { "0", "1", "2", "3", "4", "5",
            "6", "7", "8", "9", "A", "B", "C", "D", "E", "F" };

    /**
     * 盐值
     */
    private static final String SALT = "9807gf34k5676#$$%fd!yy==&^)!--";

    /**
     * 转换字节数组为16进制字串
     *
     * @param b
     *            字节数组
     * @return 16进制字串
     */
    public static String byteArrayToHexString(byte[] b) {
        StringBuffer resultSb = new StringBuffer();
        for (int i = 0; i < b.length; i++) {
            resultSb.append(byteToHexString(b[i]));
        }
        return resultSb.toString();
    }

    private static String byteToHexString(byte b) {
        int n = b;
        if (n < 0)
            n = 256 + n;
        int d1 = n / 16;
        int d2 = n % 16;
        return hexDigits[d1] + hexDigits[d2];
    }

    private static String encodeMd5(String key) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.reset();
            md.update(key.getBytes());
            byte[] bytes = md.digest();
            String result = byteArrayToHexString(bytes);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String getMD5(String origin) {
        String resultString = SALT + "_" + origin;
        resultString = encodeMd5(resultString);
        resultString = origin + resultString + "_" + SALT;
        resultString = encodeMd5(resultString);
        return resultString;
    }

    public static String getMD5(String origin, String salt) {
        String resultString = SALT + "_" + origin + "_" + salt;
        resultString = encodeMd5(resultString);
        resultString = origin + "{" + salt + resultString + "}_" + SALT;
        resultString = encodeMd5(resultString);
        return resultString;
    }

    /**
     * 对于InputStream，来获取其所对应的md5值
     * @param in
     * @return
     */
    public static String getInputStreamMd5(final InputStream in){
        byte buffer[] = new byte[1024];
        int len;
        try {
            MessageDigest digest = MessageDigest.getInstance("MD5");
            while ((len = in.read(buffer, 0, 1024)) != -1) {
                digest.update(buffer, 0, len);
            }
            in.close();
            byte[] b = digest.digest();
            return byteArrayToHexString(b);
        } catch (Exception e) {
            return null;
        }
    }

    public static String getMd5ForBytes(final byte[] bytes){
        try{
            MessageDigest digest = MessageDigest.getInstance("MD5");
            byte buffer[] = new byte[1024];
            int len;
            ByteArrayInputStream bIn = new ByteArrayInputStream(bytes);
            while ((len = bIn.read(buffer, 0, 1024)) != -1) {
                digest.update(buffer, 0, len);
            }
            bIn.close();
            return byteArrayToHexString(digest.digest());
        }catch (Exception e) {
            return null;
        }
    }

    public static String getFileMd5(final File file) {
        FileInputStream in = null;
        byte buffer[] = new byte[1024];
        int len;
        try {
            MessageDigest digest = MessageDigest.getInstance("MD5");
            in = new FileInputStream(file);
            while ((len = in.read(buffer, 0, 1024)) != -1) {
                digest.update(buffer, 0, len);
            }
            in.close();
            byte[] b = digest.digest();
            return byteArrayToHexString(b);
        } catch (Exception e) {
            return null;
        }
    }
    
}
